package org.moonshiner.controller.security.signature;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

public class SignVerificator {

	public void verify() {

		FileInputStream fileInputStream = null;
		byte[] encKey = null;
		try {
			fileInputStream = new FileInputStream("publicKey.key");
			encKey = new byte[fileInputStream.available()];
			fileInputStream.read(encKey);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encKey);

		KeyFactory keyFactory = null;
		try {
			keyFactory = KeyFactory.getInstance("DSA", "SUN");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}

		PublicKey publicKey = null;
		try {
			publicKey = keyFactory.generatePublic(publicKeySpec);
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		}

		FileInputStream signFileInputStream = null;
		byte[] signToVerify = null;
		try {
			signFileInputStream = new FileInputStream("signature.sig");
			signToVerify = new byte[signFileInputStream.available()];
			signFileInputStream.read(signToVerify);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				signFileInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		Signature signature = null;

		try {
			signature = Signature.getInstance("SHA1withDSA", "SUN");
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		}

		try {
			signature.initVerify(publicKey);
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		}

		FileInputStream dataInputStream = null;
		try {
			dataInputStream = new FileInputStream("1.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		BufferedInputStream bufferedInputStream = new BufferedInputStream(
				dataInputStream);
		byte[] buffer = new byte[1024];
		int length = 0;

		try {
			while (bufferedInputStream.available() != 0) {
				length = bufferedInputStream.read(buffer);
				signature.update(buffer, 0, length);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SignatureException e) {
			e.printStackTrace();
		} finally {
			try {
				bufferedInputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		try {
			boolean verified = signature.verify(signToVerify);
			System.out.println(verified);
		} catch (SignatureException e) {
			e.printStackTrace();
		}

	}
}
